﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace SIPO.Library.DataAccess.Transaction
{
    public partial class JobDescRelationShipInternalRepo
    {
        public List<JobDescRelationShipInternal> GetListByJobDescID(int JobDescID,int programID)
        {
            List<JobDescRelationShipInternal> tt = new List<JobDescRelationShipInternal>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT JobDescRelationShipID, JobDescriptionID, Department, Purpose, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate FROM transactions.JobDescRelationShipInternal WHERE JobDescriptionID=@JobDescriptionID AND ProgramID=@ProgramID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@JobDescriptionID", SqlDbType.Int).Value = JobDescID;
                    cmd.Parameters.Add("@programID", SqlDbType.Int).Value = programID;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                JobDescRelationShipInternal t = new JobDescRelationShipInternal();
                                t.JobDescRelationShipID = dr[JobDescRelationShipExternal.Fields.JobDescRelationShipID] == DBNull.Value ? 0 : int.Parse(dr[JobDescRelationShipExternal.Fields.JobDescRelationShipID].ToString());
                                t.JobDescriptionID = dr[JobDescRelationShipExternal.Fields.JobDescriptionID] == DBNull.Value ? 0 : int.Parse(dr[JobDescRelationShipExternal.Fields.JobDescriptionID].ToString());
                                t.Department = dr[JobDescRelationShipExternal.Fields.Department] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.Department].ToString();
                                t.Purpose = dr[JobDescRelationShipExternal.Fields.Purpose] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.Purpose].ToString();
                                t.CreatedBy = dr[JobDescRelationShipExternal.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[JobDescRelationShipExternal.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[JobDescRelationShipExternal.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[JobDescRelationShipExternal.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[JobDescRelationShipExternal.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[JobDescRelationShipExternal.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }


        public List<JobDescRelationShipInternal> GetListByJobDescID(int JobDescID)
        {
            List<JobDescRelationShipInternal> tt = new List<JobDescRelationShipInternal>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT JobDescRelationShipID, JobDescriptionID, Department, Purpose, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate FROM transactions.JobDescRelationShipInternal WHERE JobDescriptionID=@JobDescriptionID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@JobDescriptionID", SqlDbType.Int).Value = JobDescID;
                   
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                JobDescRelationShipInternal t = new JobDescRelationShipInternal();
                                t.JobDescRelationShipID = dr[JobDescRelationShipExternal.Fields.JobDescRelationShipID] == DBNull.Value ? 0 : int.Parse(dr[JobDescRelationShipExternal.Fields.JobDescRelationShipID].ToString());
                                t.JobDescriptionID = dr[JobDescRelationShipExternal.Fields.JobDescriptionID] == DBNull.Value ? 0 : int.Parse(dr[JobDescRelationShipExternal.Fields.JobDescriptionID].ToString());
                                t.Department = dr[JobDescRelationShipExternal.Fields.Department] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.Department].ToString();
                                t.Purpose = dr[JobDescRelationShipExternal.Fields.Purpose] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.Purpose].ToString();
                                t.CreatedBy = dr[JobDescRelationShipExternal.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[JobDescRelationShipExternal.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[JobDescRelationShipExternal.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[JobDescRelationShipExternal.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[JobDescRelationShipExternal.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[JobDescRelationShipExternal.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[JobDescRelationShipExternal.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		


    }
}
